package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {


    @Autowired
    private CategoryService CategoryService;

    @PostMapping
    public R<String> save(@RequestBody Category category){
        if(StringUtils.isNotEmpty(category.toString())){
            CategoryService.save(category);

            return R.success("保存成功");
        }else{
            return R.success("保存失败");
        }

    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize )
    {
        Page pageInfo = new Page(page, pageSize);

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Category::getSort);
        CategoryService.page(pageInfo, wrapper);
        return R.success(pageInfo);
    }
    @PutMapping
    public R<String> update(@RequestBody Category category){
        CategoryService.updateById(category);

        return R.success("成功修改了");
    }

    /**
     * 为什么这里不能用/{id}来接收参数，反而直接接受参数了？？可以深究一下
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(long ids){
        //CategoryService.removeById(ids);
        CategoryService.remove(ids);
        return R.success("成功删除了");
    }

    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        //####################这里要加泛型
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper();
        wrapper.eq(category.getType() != null,Category::getType,category.getType());
        wrapper.orderByDesc(Category::getSort);
        List<Category> list = CategoryService.list(wrapper);
        return R.success(list);
    }
}
